how to apply a function to each category